<template>
    <!-- 添加或修改生产订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-tabs v-model="activeName" class="detail-tab" type="card">
                <el-tab-pane label="基本信息" name="first">
                    <el-row :gutter="24">
                      <!--<el-col :span="12">
                          <el-form-item label="采购方总部编码" prop="purchaserHqCode">
                              <el-input v-model="form.purchaserHqCode" placeholder="请输入采购方总部编码"/>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12">
                          <el-form-item label="供应商编码" prop="supplierCode">
                              <el-input v-model="form.supplierCode" placeholder="请输入供应商编码"/>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12">
                          <el-form-item label="供应商名称" prop="supplierName">
                              <el-input v-model="form.supplierName" placeholder="请输入供应商名称"/>
                          </el-form-item>
                      </el-col>-->
                        <el-col :span="12">
                            <el-form-item label="生产订单号" prop="ipoNo">
                                <el-input v-model="form.ipoNo" placeholder="请输入生产订单号"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="订单类型" prop="ipoType">
                            <el-select v-model="form.ipoType" placeholder="请选择订单类型" clearable >
                              <el-option v-for="dict in dict.type.ipo_type"
                                         :key="dict.value" :label="dict.label" :value="dict.value"/>
                            </el-select>
                          </el-form-item>
                        </el-col>
                    </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="品类编码" prop="categoryCode">
                                <el-input v-model="form.categoryCode"  placeholder="请输入品类编码" disabled/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="种类编码" prop="subclassCode">
                                <el-input v-model="form.subclassCode" placeholder="请输入种类编码" disabled/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                          <el-form-item label="排产计划编码" prop="scheduleCode">
                            <!--  <el-input v-model="form.scheduleCode" placeholder="请输入排产计划编码"/>-->
                            <!--<el-select v-model="form.scheduleCode" placeholder="请选择排产计划" filterable clearable multiple
                                       style="width: 300px">
                              <el-option v-for="item in scheduleCodeList"
                                         :key="item.id" :label="item.scheduleCode" :value="item.scheduleCode"/>
                            </el-select>-->
                            <el-select v-model="form.scheduleCode" placeholder="请选择排产计划编码" filterable @change="selectScheduleCode">
                              <el-option v-for="item in scheduleCodeList"  ref="scheduleCodeOption"
                                         :key="item.id" :label="item.scheduleCode" :value="item.scheduleCode"/>
                            </el-select>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="订单关联类型" prop="dataType">
                            <el-select v-model="form.dataType" placeholder="请选择订单关联类型" clearable >
                              <el-option v-for="dict in dict.type.factory_data_type"
                                         :key="dict.value" :label="dict.label" :value="dict.value"/>
                            </el-select>
                          </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="采购订单行项目" prop="poItemIds" v-if="form.dataType==1">
                               <!-- <el-input v-model="form.poItemId" placeholder="请输入采购订单行项目"disabled/>-->
                                  <el-select v-model="form.poItemIds" placeholder="请选择产品" filterable clearable multiple style="width: 100%">
                                    <el-option v-for="item in goodsList"
                                               :key="item.id" :label="item.goodsName" :value="item.id"/>
                                  </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="销售订单号" prop="poItemId" v-if="form.dataType==2">
                            <el-input v-model="form.poItemId" placeholder="请输入销售订单号"disabled/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="销售订单行项目编号" prop="soItemNo" v-if="form.dataType==2">
                                <el-input v-model="form.soItemNo" placeholder="请输入销售订单行项目编号" disabled/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <!--<el-col :span="12">
                            <el-form-item label="物资id分组" prop="productIdGrpNo">
                                <el-input v-model="form.productIdGrpNo" placeholder="请输入物资id分组"/>
                            </el-form-item>
                        </el-col>-->
                        <el-col :span="12">
                          <el-form-item label="产品型号" prop="productModel">
                            <el-input v-model="form.productModel" placeholder="请输入产品型号"/>
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="厂家物料编码" prop="materialsCode">
                                <el-input v-model="form.materialsCode" placeholder="请输入厂家物料编码"/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="厂家物料名称" prop="materialsName">
                                <el-input v-model="form.materialsName" placeholder="请输入厂家物料名称"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="厂家物资单位" prop="materialsUnit">
                                <el-input v-model="form.materialsUnit" placeholder="请输入厂家物资单位"/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="厂家物料描述" prop="materialsDesc">
                                <el-input v-model="form.materialsDesc" placeholder="请输入厂家物料描述"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="生产数量" prop="amount">
                                <el-input v-model="form.amount" placeholder="请输入生产数量"/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="计量单位" prop="unit">
                                <el-input v-model="form.unit" placeholder="请输入计量单位"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="计划开始日期" prop="planStartDate">
                                <!--<el-input v-model="form.planStartDate" placeholder="请输入计划开始日期"/>-->
                                <el-date-picker clearable
                                                v-model="form.planStartDate"
                                                type="datetime"
                                                value-format="yyyy-MM-dd HH:mm:ss"
                                                placeholder="请输入计划完成日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="计划完成日期" prop="planFinishDate">
                               <!-- <el-input v-model="form.planFinishDate" placeholder="请输入计划完成日期"/>-->
                              <el-date-picker clearable
                                              v-model="form.planFinishDate"
                                              type="datetime"
                                              value-format="yyyy-MM-dd HH:mm:ss"
                                              placeholder="请输入计划完成日期">
                              </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="实际开始日期" prop="actualStartDate">
                                <!--<el-input v-model="form.actualStartDate" placeholder="请输入实际开始日期"/>-->
                              <el-date-picker clearable
                                              v-model="form.actualStartDate"
                                              type="datetime"
                                              value-format="yyyy-MM-dd HH:mm:ss"
                                              placeholder="请输入实际开始日期">
                              </el-date-picker>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="实际完成日期" prop="actualFinishDate">
                              <!--  <el-input v-model="form.actualFinishDate" placeholder="请输入实际完成日期"/>-->
                              <el-date-picker clearable
                                              v-model="form.actualFinishDate"
                                              type="datetime"
                                              value-format="yyyy-MM-dd HH:mm:ss"
                                              placeholder="请输入实际完成日期">
                              </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="生产工厂名称" prop="plantName">
                                <el-input v-model="form.plantName" placeholder="请输入生产工厂名称"/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="12">
                            <el-form-item label="生产车间名称" prop="workshopName">
                                <el-input v-model="form.workshopName" placeholder="请输入生产车间名称"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="生产中心" prop="center">
                                <el-input v-model="form.center" placeholder="请输入生产中心"/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24">
                        <el-col :span="24">
                          <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注"/>
                          </el-form-item>
                        </el-col>
                        <!--<el-col :span="12">
                            <el-form-item label="数据来源" prop="dataSource">
                                <el-input v-model="form.dataSource" placeholder="请输入数据来源"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="来源数据创建时间" prop="dataSourceCreateTime">
                                <el-input v-model="form.dataSourceCreateTime" placeholder="请输入来源数据创建时间"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="数据拥有方" prop="ownerId">
                                <el-input v-model="form.ownerId" placeholder="请输入数据拥有方"/>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="数据可见方" prop="openId">
                                <el-input v-model="form.openId" placeholder="请输入数据可见方"/>
                            </el-form-item>
                        </el-col>-->
                    </el-row>
                </el-tab-pane>
            </el-tabs>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addAction, getById, updateAction,getAction } from '@/api/manage';
    import moment from 'moment';

    export default {
        name: "FactoryProduceMainForm",
       dicts: ['factory_data_type','ipo_type'],
        data() {
            return {
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                },
                queryParams: {
                  pageNum: 1,
                  pageSize: 10000,
                  projectId: null,
                },
                queryParamsSale: {
                  pageNum: 1,
                  pageSize: 10000,
                  projectId: null,
                  isdo: "1",
                  params:{},
                },
                scheduleCodeList: [],
                url: {
                  getById:'/factoryProduceMain/factoryProduceMain/',
                  add:'/factoryProduceMain/factoryProduceMain/add',
                  edit:'/factoryProduceMain/factoryProduceMain/edit',
                  factoryProducePlanUrl:'/factoryProducePlan/factoryProducePlan/list',
                  goodsList: '/goodsSale/goodsSale/list',
                },
                goodsList: [],
            };
        },
        created() {

        },
        methods: {
            selectScheduleCode(value){
              let that = this;
              that.$nextTick(() => {
                let arr = that.$refs.scheduleCodeOption;
                for (let i = 0; i < arr.length; i++) {
                  if (arr[i].currentValue == value) {
                    that.form.planStartDate = this.scheduleCodeList[i].planStartDate;
                    that.form.planFinishDate =  this.scheduleCodeList[i].planFinishDate;
                    that.form.actualStartDate =  this.scheduleCodeList[i].actualStartDate;
                    that.form.actualFinishDate =  this.scheduleCodeList[i].actualFinishDate;
                  }
                }
              })
            },
            getScheduleCodeList() {
              getAction(this.url.factoryProducePlanUrl,this.queryParams ).then(response => {
                this.scheduleCodeList = response.rows;
              });
            },
            getGoodsList() {
              getAction(this.url.goodsList, this.queryParamsSale).then(response => {
                this.goodsList = response.rows;
              });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                  id: null,
                  status: "0",
                  remark: null,
                  createBy: null,
                  createTime: null,
                  updateBy: null,
                  updateTime: null,
                  delFlag: null,
                  purchaserHqCode: null,
                  ipoType: "0",
                  supplierCode: null,
                  supplierName: null,
                  ipoNo: null,
                  categoryCode: "60",
                  subclassCode: "60004",
                  scheduleCode: null,
                  poItemId: null,
                  dataType: "1",
                  soItemNo: null,
                  materialsCode: null,
                  materialsName: null,
                  materialsUnit: null,
                  materialsDesc: null,
                  amount: null,
                  unit: null,
                  productIdGrpNo: null,
                  productIdType: null,
                  productModel: null,
                  planStartDate: null,
                  planFinishDate: null,
                  actualStartDate: null,
                  actualFinishDate: null,
                  plantName: "山东鲁能光大电力器材有限公司",
                  workshopName: null,
                  ipoStatus: "0",
                  center: null,
                  dataSource: null,
                  dataSourceCreateTime: null,
                  ownerId: null,
                  openId: null,
                  projectCode: null,
                  projectId: null,
                  poItemIds:[],
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            handleAdd(projectId,projectCode) {
                this.reset();
                const now = new Date();
                const year = now.getFullYear(); // 年份，例如：2021
                const month = now.getMonth() + 1; // 月份，范围是0-11，需要+1，例如：1月为1
                const day = now.getDate(); // 日期，例如：31
                const hours = now.getHours(); // 小时，24小时制，例如：23
                const minutes = now.getMinutes(); // 分钟，例如：59
                const seconds = now.getSeconds(); // 秒，例如：30
                this.form.ipoNo="DING"+year+month+day+hours+minutes+seconds;
                this.form.projectCode=projectCode;
                this.form.projectId=projectId;
                this.queryParams.projectId=projectId;
                this.queryParamsSale.params["projectId"] = projectId;
                this.open = true;
                this.title = "添加生产订单信息";
                this.getScheduleCodeList();
                this.getGoodsList();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改生产订单信息";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.add, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },
        }
    };
</script>
